package com.shengapp.models;

/**
 * @Author: sheng-app
 * @Date: 2021/7/26 15:45
 */

import org.json.JSONArray;
import org.json.JSONObject;

public class ApiRet extends JSONObject {
    public static final int CODE_PARAMETER = 1000;
    public static final int CODE_TOKEN = 1001;
    public static final int CODE_ROLE = 1002;
    public static final int CODE_LOGIC = 1003;
    public static final int CODE_SYSTEM = 1004;
    private static final String CODE = "code";
    private static final String SUB_CODE = "subCode";
    private static final String MESSAGE = "message";
    private static final String DATA = "data";
    private static final String PARAMS = "params";
    private static final String SUCCESS = "success";

    public ApiRet() {
    }

    public ApiRet(String apiRetContent) {
        super(apiRetContent);
    }

    public static ApiRet ok() {
        ApiRet apiRet = new ApiRet();
        apiRet.put("success", true);
        return apiRet;
    }

    public static ApiRet fail() {
        ApiRet apiRet = new ApiRet();
        apiRet.put("success", false);
        return apiRet;
    }

    public boolean isOk() {
        return this.getBoolean("success");
    }

    public boolean isFail() {
        return !this.getBoolean("success");
    }

    public String getMessage() {
        return super.getString("message");
    }

    public ApiRet setMessage(String message) {
        super.put("message", message);
        return this;
    }

    public int getCode() {
        return super.getInt("code");
    }

    public ApiRet setCode(int code) {
        super.put("code", code);
        return this;
    }

    public Integer getSubCode() {
        return super.optInt("subCode");
    }

    public ApiRet setSubCode(int subCode) {
        super.put("subCode", subCode);
        return this;
    }


    public JSONObject getData() {
        return super.optJSONObject("data");
    }

    public ApiRet setData(JSONObject data) {
        super.put("data", data);
        return this;
    }

    public ApiRet setData(JSONArray data) {
        super.put("data", data);
        return this;
    }

    public JSONArray getDataArray() {
        return super.optJSONArray("data");
    }


    public ApiRet putData(String key, Object value) {
        JSONObject data = this.getData();
        if (data == null) {
            data = new JSONObject();
        }

        data.put(key, value);
        this.setData(data);
        return this;
    }

    public ApiRet setSuccess(boolean success) {
        super.put("success", success);
        return this;
    }

    public ApiRet putParam(String key, Object value) {
        JSONObject params = this.getParams();
        if (params == null) {
            params = new JSONObject();
        }

        params.put(key, value);
        this.setParams(params);
        return this;
    }

    public JSONObject getParams() {
        return this.optJSONObject("params");
    }

    public ApiRet setParams(JSONObject params) {
        super.put("params", params);
        return this;
    }


}

